<?php
// merchant model
class UsersModel extends Model {
//    public $_validate	=	array(
//    	array('email','require','{%email_require}'),
//    	array('password','require','{%password_require}'),
//        );
    
	public $_validate = array(
		array('account','require','{account_require}'),
		array('password','require','{%password_require}')
	);
	
	public function accountExisted($account) {
		$w = array('account' => $account);
    	$f = array('uid');
    	$result = $this->where($w)->field($field)->find();

    	if (empty($result)) {
    		return false;
    	} else {
    		return true;
    	}
	}
	
//    public function emailExisted($email) {
//    	$w = array('email' => $email);
//    	$f = array('uid');
//    	$result = $this->where($w)->field($field)->find();
//
//    	if (empty($result)) {
//    		return false;
//    	} else {
//    		return true;
//    	}
//    }
    
    public function userExisted($uid) {
    	$w = array('uid' => $uid);
    	$f = array('uid');
    	$result = $this->where($w)->field($field)->find();
    	if (empty($result)) {
    		return false;
    	} else {
    		return true;
    	}    	
    }
    
    public function getUserInfoById($uid) {
    	return $this->where(array('uid'=>$uid))->find();
    }
    
	public function getUserInfoByAccount($account) {
    	return $this->where(array('account'=>$account))->find();
    }
    
    public function getBasicUserInfoByUid($uid) {
    	$w = array('uid' => $uid);
    	$w['statusid'] = 1;
    	return $this->getBasicUserInfo($w);
    }
    
	public function getBasicUserInfoByAccount($account) {
    	$w = array('account' => $account);
    	$w['statusid'] = 1;
    	return $this->getBasicUserInfo($w);
    }

//    public function getBasicUserInfoByEmail($email) {
//    	$w = array('email' => $email);
//    	$w['statusid'] = 1;
//    	return $this->getBasicUserInfo($w);
//    }
    
    public function getBasicUserInfo($where) {
    	$f = array('uid','account', 'email', 'password','firstname', 'secondname', 'lastname', 'avatar', 'birthday', 'deathday','twodimcode','relationship', 'visiable', 'accesspwd', 'pwdhint', 'remark', 'confirmid', 'paid','createdby');
    	$userInfo = $this->where($where)->field($f)->find();
    	if ($userInfo != null) {
    		$userInfo['birthday'] = substr($userInfo['birthday'], 0, 11);
    		$userInfo['deathday'] = substr($userInfo['deathday'], 0, 11);
    		$by = substr($userInfo['birthday'], 0, 4);
    		$dy = substr($userInfo['deathday'], 0, 4);
    		$userInfo['age'] = $dy - $by;
    	}
    	
		return $userInfo;
    }
    
    public function getGenealogyUserName($uid) {
    	$Genealogy = M('Genealogy');
    	$w = array('uid' => $uid);
    	$f = array('firstname', 'lastname');
    	$result = $Genealogy->where($w)->field($f)->select();
    	if($result === false) {
    		return false;
    	}
    	 
    	if ($result != null) {
    		$name = '';
    		foreach($result as $v) {
    			//print_r($v);
    			$name = $name .$v['firstname'].' '.$v['lastname'].'   ';
    		}
    		return $name;
    	} else {
    		return null;
    	}
    }

//    public function getGenealogyTree($uid) {
//    	
//    }

    public function getUserAccount($uid) {
    	$w = array('uid' => $uid);
    	$f = array('account');
    	$result = $this->where($w)->field($f)->find();
    	return $result['account'];
    }
    
    public function getUserEmail($uid) {
    	$w = array('uid' => $uid);
    	$f = array('email');
    	$result = $this->where($w)->field($f)->find();
    	return $result['email'];
    }
    
    
    public function getUserRemark($uid) {
    	$w = array('uid' => $uid);
    	$f = array('uid', 'remark');
    	return $this->where($w)->field($f)->find();
    }
    
    public function getPicturesByUid($uid) {
    	$Pictures = D('UserPictures');
    	$w = array('uid' => $uid);
    	$f = array('id', 'name');
    	$o = array('createdtime' => 'desc');
    	$pics = $Pictures->where($w)->order($o)->field($f)->select();
    	return $pics;
    }
    
    public function getCatchwordsByUid($uid) {
    	$Catchword = D('UserCatchword');
    	$w = array('uid' => $uid);
    	$f = array('id', 'content');
    	$o = array('createdtime' => 'desc');
    	$words = $Catchword->where($w)->order($o)->field($f)->select();
    	return $words;
    }
    
    public function getCount() {
    	return $this->count();
    }
    
    public function delCatchwordById($id) {
    	$Catchword = D('UserCatchword');
    	$w = array('id' => $id);
    	return $Catchword->where($w)->delete();
    }
    
    public function updateCatchword($id, $content) {
    	$Catchword = D('UserCatchword');
    	$data = array('id' => $id, 'content' => $content);
    	return $Catchword->save($data);
    }
    
    public function getAlllife($uid) {
    	$allLife = D('UserAlllife');
    	$w = array('uid' => $uid);
    	$f = array('id', 'content');
    	$o = array('createdtime' => 'desc');
    	$result = $allLife->where($w)->order($o)->field($f)->find();
    	return $result;
    }
    
    public function insertUserAgency($data) {
    	$userAgency = M('UserAgency');
    	return $userAgency->add($data);
    }
    
    public function hasConfirmed($uid) {
    	$w = array('uid' => $uid);
    	$f = array('confirmid');
     	$result = $this->where($w)->field($f)->find();
     	if($result['confirmid'] == 1) {
     		return true;
     	} else {
     		return false;
     	}
    }
    
    public function createDCode($uid) {
    	$accoun = $this->getUserAccount($uid);
    	
    	$viewUrl = 'http://'.$_SERVER['HTTP_HOST'].'/Users/userView?uid='.$uid;
    	try {
    		$pngAbsoluteFilePath = './Uploads/';
    		$filename = date("YmdHis") . '_' . rand(10000, 99999) . '.png';
    		$pngAbsoluteFilePath = $pngAbsoluteFilePath .$filename;
    		 
    		vendor('phpqrcode.phpqrcode');
    		$png = QRcode::png($viewUrl, $pngAbsoluteFilePath, 'QR_ECLEVEL_H');
    		return substr($pngAbsoluteFilePath, 1, strlen($pngAbsoluteFilePath));
    	} catch(Exception $ex){
    		return false;
    	}
    }
    
    public function checkoutComplete($uid) {
//    	$email = $this->getUserEmail($uid);
//		$data = array('uid' => $uid, 'email' => $email,'paid' => 1);
		$account = $this->getUserAccount($uid);
		$email = $this->getUserEmail($uid);
		$data = array('uid' => $uid, 'account' => $account, 'email' => $email, 'paid' => 1);
		return $this->save($data);
    }
    
    function sendEmail($userInfo) {
    	if (empty($userInfo)) {
    		return false;
    	}
    
    	$uid = encryptUid($userInfo['uid']);
    	$url = 'http://'.$_SERVER['HTTP_HOST'].'/Users/userView?uid='.$uid;
    	$logo = 'http://'.$_SERVER['HTTP_HOST'].'/Public/images/logo.png';
    	$twodimcode = 'http://'.$_SERVER['HTTP_HOST'].$userInfo['twodimcode'];
    	$auth = array(L('auth_private'), L('auth_semi_public'), L('auth_public'));
    
    	load('extend');
    	$body = "<table><tr><td><img src='$logo' /></td></tr>".
    			$body = $body. "<tr><td><p>Bonjour, Cher client</p><p>L’ensemble de l’équipe Offrez-lui un sourire éternel vous remercie d’avoir créé une page pour <strong>".$userInfo['lastname']." ".$userInfo['firstname']."</strong></p></td></tr>";
    	$body = $body. "<tr><td>Vous pouvez dès à présent communiquer à vos proches le lien ci-dessous permettant d’accéder au site :</td><tr>";
    	$body = $body. "<tr><td><strong><a href='$url'>$url</a></strong></td></tr>";
    	$body = $body. "<tr><td><img src='$twodimcode' /> </td></tr>";
    	$body = $body. "<tr><td>Pour modifier une page, cliquez sur le lien ci-dessous puis sur « Modifier la page », et saisissez vos identifiants :</td></tr>";
    	$body = $body. "<tr><td>Email :<strong>". $userInfo['email']."</strong></td></tr>";
    	$body = $body. "<tr><td>Mot de passe :<strong>".$_SESSION['password_not_md5']."</strong></td></tr>";
    	$body = $body. "<tr><td>L’accès : <strong>".$auth[$userInfo['visiable']]."</strong></td></tr>";
    	$body = $body. "<tr><td>Mot de passe pour visiteur : <strong>".$_SESSION['accesspwd_not_md5']."</strong></td></tr>";
    	$body = $body. "<tr><td>Hint :<strong>".$userInfo['pwdhint']."</strong></td></tr>";
    	$body = $body. "<tr><td><p>Pour toute question, n’hésitez pas à nous contacter directement à l’adresse suivante : </p>
				.<p>contact@unsourireternel.fr.</p></td></tr>";
    	$body = $body. "<tr><td>Nous vous remercions pour votre confiance,</td></tr>";
    	$body = $body. "<tr><td>L’équipe Offrez-lui un Sourire Eternel.</td></tr>";
    	$body = $body. "</table>";
    
    	$toname = $userInfo['lastname'].'  '. $userInfo['firstname'];
    	$subject = L('regist_email_subject');
    	$result = think_send_mail($userInfo['email'], $toname, $subject, $body);
    
    	if ($result === true) {
    		$_SESSION['password_not_md5'] = '';
    		$_SESSION['accesspwd_not_md5'] = '';
    
    	}
    	return $result;
    }
    
    public function deleteUsersByIds($ids) {
    	if (!isset($ids) || empty($ids)){
    		return false;
    	}
    	 
    	if(is_array($ids)){
    		if(count($ids) > 1) {
    			$w = 'uid in('.implode(',',$ids).')';
    		} else if (count($ids) == 1) {
    			$w = 'uid='.$ids[0];
    		}
    	}
    
    	return $this->where($w)->delete();
    }
}